package malus.shaofeng.com.beiwomusic.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Environment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import malus.shaofeng.com.beiwomusic.R;
import malus.shaofeng.com.beiwomusic.utils.BitmapHelper;
import malus.shaofeng.com.beiwomusic.utils.Constants;
import malus.shaofeng.com.beiwomusic.utils.FileSizeUtil;
import malus.shaofeng.com.beiwomusic.view.SildingFinishLayout;
import malus.shaofeng.com.beiwomusic.view.Topbar;

@ContentView(R.layout.activity_space_setting)
public class SpaceSettingActivity extends Activity implements SildingFinishLayout.OnSildingFinishListener, Topbar.onTopbarClickListener, View.OnClickListener {
    @ViewInject(R.id.setting_sildingFinishLayout)
    SildingFinishLayout sildingFinishLayout;
    @ViewInject(R.id.space_setting_clear_cache)
    LinearLayout clearCache;
    @ViewInject(R.id.space_setting_check_version)
    LinearLayout checkVersion;
    @ViewInject(R.id.space_setting_put_result)
    LinearLayout putResult;
    @ViewInject(R.id.space_setting_disclaimer)
    LinearLayout disclaimer;
    @ViewInject(R.id.topbar)
    Topbar topbar;
    @ViewInject(R.id.space_setting_file_size)
    TextView textFilsSize;
    public static final int MSG_CLEAR_CACHE=1;
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case MSG_CLEAR_CACHE:
                    dialog.cancel();
                    Toast.makeText(
                            SpaceSettingActivity.this,
                            "已成功清除缓存",
                            Toast.LENGTH_SHORT).show();
                    textFilsSize.setText(getFileSize() + "M");
                    break;
            }
        }
    };
    private Dialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        //进度环
        dialog = new Dialog(SpaceSettingActivity.this);
        dialog.setContentView(R.layout.item_running);
        dialog.getWindow().setBackgroundDrawableResource(R.drawable.transaction);
        dialog.setCancelable(false);

        sildingFinishLayout.setOnSildingFinishListener(this);
        topbar.setOnTopbarClickListener(this);
        clearCache.setOnClickListener(this);
        checkVersion.setOnClickListener(this);
        putResult.setOnClickListener(this);
        disclaimer.setOnClickListener(this);
        initData();
    }

    private void initData() {
        textFilsSize.setText(getFileSize() + "M");
    }

    /**
     * 获取缓存文件大小
     *
     * @return
     */
    private double getFileSize() {
        File directory = Environment.getExternalStorageDirectory();
        File file = new File(directory, Constants.File.BITMAP_HELPER_CACHE);
        double fileSize = 0;
        if (file.exists()) {
            fileSize = FileSizeUtil.getFileOrFilesSize(file.getAbsolutePath(), FileSizeUtil.SIZETYPE_MB);
        }
        return fileSize;
    }

    @Override
    public void onSildingFinish() {
        finish();
        overridePendingTransition(0, R.anim.trans_slider_out);
    }

    @Override
    public void onBackPressed() {
        finish();
        overridePendingTransition(0, R.anim.trans_slider_out);
    }

    @Override
    public void onLeftButtonClick() {
        finish();
        overridePendingTransition(0, R.anim.trans_slider_out);
    }

    @Override
    public void onRightButtonClick() {

    }

    @Override
    public void onSearchButtonClick() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.space_setting_clear_cache:
                if(getFileSize()<=0.0001){
                    Toast.makeText(SpaceSettingActivity.this,"亲，已经没有缓存了~",Toast.LENGTH_SHORT).show();
                }
                dialog.show();
                //清除缓存
                BitmapUtils bitmapUtils = BitmapHelper.getBitmapUtils();
                bitmapUtils.clearCache();
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        handler.sendEmptyMessage(1);
                    }
                }, 2000);
                break;
            case R.id.space_setting_check_version:
                Toast.makeText(SpaceSettingActivity.this,"已经是最新版本了",Toast.LENGTH_SHORT).show();
                break;
            case R.id.space_setting_put_result:
                startActivity(new Intent(this,SettingReturnActivity.class));
                overridePendingTransition(R.anim.trans_slider_in,0);
                break;
            case R.id.space_setting_disclaimer:
                startActivity(new Intent(this,SettingDisclarmerActivity.class));
                overridePendingTransition(R.anim.trans_slider_in,0);
                break;
        }
    }
}
